﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace BlueLotus7.ArchiveSystem.SinaContentApi
{
    public class UrlGenerator
    {
        public const string host = "http://weibo.cn";
        protected const string versionParam = "vt=4";
        protected const string tfValue = "tf=6_007";
        protected string gsid = string.Empty;

        #region Constructor

        public UrlGenerator(string gsid)
        {
            this.gsid = gsid;
        }

        #endregion

        public void ResetGSID(string gsid)
        {
            this.gsid = gsid;
        }

        protected string AppendGSID(string url)
        {
            if (!string.IsNullOrWhiteSpace(url))
            {
                url = url + "&gsid=" + this.gsid;
            }
            return url;
        }

        #region  Home

        protected const string weiboHome = host + "/u/{0}?page={1}";
        public static string GenerateWeiboHomeUrl(long weiboUserId, int page = 1)
        {
            string uri = null;

            if (weiboUserId > 0 && page > 0)
            {
                uri = string.Format(weiboHome, weiboUserId.ToString(), page.ToString());
            }

            return uri;
        }

        #endregion

        #region  User Info

        /// <summary>
        /// Follows
        /// </summary>
        protected const string followMembers = host + "/{0}/follow?page={1}&" + versionParam;
        public static string GenerateWeiboFollowListUrl(long weiboUserId, int page = 1)
        {
            string uri = null;

            if (weiboUserId > 0 && page > 0)
            {
                uri = string.Format(followMembers, weiboUserId.ToString(), page.ToString());
            }

            return uri;
        }

        /// <summary>
        /// Fans
        /// </summary>
        protected const string fansMembers = host + "/{0}/fans?page={1}&" + versionParam;
        public static string GenerateWeiboFansListUrl(long weiboUserId, int page = 1)
        {
            string uri = null;

            if (weiboUserId > 0 && page > 0)
            {
                uri = string.Format(fansMembers, weiboUserId.ToString(), page.ToString());
            }

            return uri;
        }

        /// <summary>
        /// User Info
        /// </summary>
        protected const string userInfo = host + "/{0}/info?" + versionParam;
        public static string GenerateWeiboUserInfoUrl(long weiboUserId)
        {
            string uri = null;

            if (weiboUserId > 0)
            {
                uri = string.Format(userInfo, weiboUserId.ToString());
            }

            return uri;
        }

        /// <summary>
        /// User Home
        /// </summary>
        protected const string userHome = host + "/{0}";
        public static string GenerateWeiboUserHomeUrl(long weiboUserId)
        {
            string uri = null;

            if (weiboUserId > 0)
            {
                uri = string.Format(userHome, weiboUserId.ToString());
            }

            return uri;
        }

        /// <summary>
        /// User Tags
        /// </summary>
        protected const string userTags = host + "/account/privacy/tags/?uid={0}";
        public static string GenerateWeiboUserTagsUrl(long weiboUserId)
        {
            string uri = null;

            if (weiboUserId > 0)
            {
                uri = string.Format(userTags, weiboUserId.ToString());
            }

            return uri;
        }

        /// <summary>
        /// Sina qun
        /// </summary>
        protected const string qunList = host + "/{0}/qun?" + tfValue + "&" + versionParam;
        public static string GenerateWeiboQunListUrl(long weiboUserId)
        {
            string uri = null;

            if (weiboUserId > 0)
            {
                uri = string.Format(qunList, weiboUserId.ToString());
            }

            return uri;
        }

        /// <summary>
        /// Album List
        /// </summary>
        protected const string albumList = host + "/{0}/photo?" + tfValue + "&" + versionParam;
        public static string GenerateAlbumListUrl(long weiboUserId)
        {
            string uri = null;

            if (weiboUserId > 0)
            {
                uri = string.Format(albumList, weiboUserId.ToString());
            }

            return uri;
        }

        #endregion

        #region Qun

        /// <summary>
        /// Sina qun home
        /// </summary>
        protected const string qunHome = host + "/dpool/ttt/grouphome.php?groupid={0}&" + versionParam;
        public static string GenerateWeiboQunHomeUrl(long qunId)
        {
            string uri = null;

            if (qunId > 0)
            {
                uri = string.Format(qunHome, qunId.ToString());
            }

            return uri;
        }

        /// <summary>
        /// User Quns
        /// </summary>
        protected const string userQunList = host + "/{0}/qun?" + versionParam;
        public static string GenerateWeiboUserQunListUrl(long weiboUserId)
        {
            string uri = null;

            if (weiboUserId > 0)
            {
                uri = string.Format(userQunList, weiboUserId.ToString());
            }

            return uri;
        }

        /// <summary>
        /// Qun article
        /// </summary>
        protected const string qunArticleCommentList = host + "/dpool/ttt/grouphome.php?act=view&gmid={0}&groupid={1}&rl=2";
        public static string GenerateWeiboQunArticleCommentListUrl(string qunArticleId, long qunId)
        {
            string uri = null;

            if (qunId > 0 && !string.IsNullOrWhiteSpace(qunArticleId))
            {
                uri = string.Format(qunArticleCommentList, qunArticleId, qunId.ToString());
            }

            return uri;
        }

        #endregion

        #region Search

        /// <summary>
        /// Article list which is involved with specific user
        /// </summary>
        protected const string involvedArticlesByAt = host + "/at/weibo?uid={0}&page={1}";
        public static string GenerateInvolvedAtArticlesUrl(long weiboUserId, int page = 1)
        {
            string uri = null;

            if (weiboUserId > 0)
            {
                uri = string.Format(involvedArticlesByAt, weiboUserId.ToString(), page.ToString());
            }

            return uri;
        }

        /// <summary>
        /// Search users via tags
        /// </summary>
        protected const string searchUsersByTags = host + "/search/?keyword={0}&stag=1&page={1}";
        public static string GenerateSearchUsersByTagsUrl(string keyword, int page = 1)
        {
            string uri = null;

            if (keyword != null && keyword.Trim().Length > 0)
            {
                uri = string.Format(searchUsersByTags, UrlEncode(keyword.Trim()), page.ToString());
            }

            return uri;
        }

        /// <summary>
        /// Search articles
        /// </summary>
        protected const string searchPostUrl = host + "/search/";
        public static string GenerateSearchArticleByCriterias()
        {
            return searchPostUrl;
        }

        #endregion

        #region Active Operations

        /// <summary>
        /// Add article to favorite
        /// </summary>
        protected const string addFavorite = host + "/fav/addFav/{0}?rl=0";
        public static string GenerateAddFavoriteUrl(string articleId)
        {
            string uri = null;

            if (!string.IsNullOrWhiteSpace(articleId))
            {
                uri = string.Format(addFavorite, articleId);
            }

            return uri;
        }

        /// <summary>
        /// Forward article page
        /// </summary>
        protected const string forwardArticle = host + "/repost/{0}?rl=0";
        public static string GenerateForwardArticlePageUrl(string articleId)
        {
            string uri = null;

            if (!string.IsNullOrWhiteSpace(articleId))
            {
                uri = string.Format(forwardArticle, articleId);
            }

            return uri;
        }

        /// <summary>
        /// Comment article page
        /// </summary>
        protected const string commentArticle = host + "/comment/{0}?rl=0";
        public static string GenerateCommentArticlePageUrl(string articleId)
        {
            string uri = null;

            if (!string.IsNullOrWhiteSpace(articleId))
            {
                uri = string.Format(commentArticle, articleId);
            }

            return uri;
        }

        #endregion

        protected static string UrlEncode(string value)
        {
            if (value != null)
            {
                value = HttpUtility.UrlEncode(value);
            }

            return value;
        }
    }
}
